package com.appsspot.client;

import java.util.ArrayList;
import com.appsspot.client.Constants;
import com.appsspot.client.parcer.SubTab;
import com.appsspot.client.parcer.Tab;
import com.appsspot.client.rpc.GreetingService;
import com.appsspot.client.rpc.GreetingServiceAsync;
import com.appsspot.client.rpc.GreetingServiceTopBar;
import com.appsspot.client.rpc.GreetingServiceTopBarAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import com.smartgwt.client.widgets.HTMLFlow;


public class Test implements EntryPoint {
	
	private ToolStrip m_toolStrip;
	private SectionStack m_leftSectionStack;
	private HTMLFlow m_htmlPanel;
	
	@Override
	public void onModuleLoad() {
		createUI();
	}

	
	/**
	 * @wbp.parser.entryPoint
	 */
	private void createUI() {		
		RootPanel rootPanel = RootPanel.get();
		
		VStack mainVStack = new VStack();
		mainVStack.setSize("100%", "100%");		
		//=============================================================
		//-------------------------TOP PANEL---------------------------
		//=============================================================
		VStack topVStack = new VStack();
		topVStack.setSize("100%", "100px");
		
		m_toolStrip = new ToolStrip();
		m_toolStrip.setSize("100%", "");		
		loadToolBarData();		
		topVStack.addMember(m_toolStrip);		
		mainVStack.addMember(topVStack);		
		//=============================================================
		//-------------------------CENTER_PANEL------------------------
		//=============================================================
		HStack centerHStack = new HStack();
		centerHStack.setSize("100%", "100%");
		//-------------------------LEFT_STACK_PANEL--------------------		
		m_leftSectionStack = new SectionStack();		
		m_leftSectionStack.setSize("200px", "100%");
		loadMainMenuData();
		centerHStack.addMember(m_leftSectionStack);
		
		m_htmlPanel = new HTMLFlow("");
		m_htmlPanel.setSize("100%", "100%");
		centerHStack.addMember(m_htmlPanel);
		mainVStack.addMember(centerHStack);
		//=============================================================
		//-------------------------LOWER_PANEL-------------------------
		//=============================================================		
		HStack lowerHStack = new HStack();
		lowerHStack.setSize("100%", "100px");
		mainVStack.addMember(lowerHStack);
		rootPanel.add(mainVStack);	
	}

	private void loadMainMenuData() {
		// Initialize the service proxy.
		GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
		

		// Set up the callback object.
		AsyncCallback<ArrayList<Tab>> callback = new AsyncCallback<ArrayList<Tab>>() {
			public void onFailure(Throwable caught) {
				RootPanel.get().setVisible(false);
			}

			@Override
			public void onSuccess(ArrayList<Tab> arrTab) {
				createMainMenu(arrTab);
			}
		};

		// Make the call to the stock price service.
		greetingService.greetServer(
				Constants.k_ATTR_SERVLET_MAIN_MENU, callback);
	}

	private void createMainMenu(ArrayList<Tab> arrTab){
		for (Tab tab : arrTab) {
			SectionStackSection sctnstcksctnNewSection = new SectionStackSection(
					Localization.getInstants().getString(tab.getName()));
			m_leftSectionStack.addSection(sctnstcksctnNewSection);

			ArrayList<SubTab> arrSubTab = tab.getSubTabs();
			for (SubTab subTab : arrSubTab) {
				Button btnNewButton = new Button(Localization.getInstants()
						.getString(subTab.getSubTabName()));
				btnNewButton.setID(subTab.getSubTabName());
				btnNewButton.setWidth("200px");
				btnNewButton.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {						
//						Button eventButton = (Button)event.getSource();
//						String eventButtonID = eventButton.getID();						
					}					
				});
				sctnstcksctnNewSection.addItem(btnNewButton);
			}
		}
	}
	
	private void loadToolBarData() {		
		GreetingServiceTopBarAsync greetingServiceTopBar = GWT.create(GreetingServiceTopBar.class);

		// Set up the callback object.
		AsyncCallback<String[][]> callback = new AsyncCallback<String[][]>() {
			public void onFailure(Throwable caught) {
				RootPanel.get().setVisible(false);
			}

			@Override
			public void onSuccess(String[][] arrTab) {
				createToolBar(arrTab);
			}
		};

		// Make the call to the stock price service.
		greetingServiceTopBar.greetServer(Constants.k_ATTR_SERVLET_TOP_BAR, callback);		
	}

	private void createToolBar(String[][] links) {
		TextItem textItem = new TextItem("newTextItem_1", Localization
				.getInstants().searchField());
		m_toolStrip.addFormItem(textItem);

		ImgButton imgButton = new ImgButton();
		imgButton.setSize("16px", "16px");
		imgButton.setSrc("ButtonSearch.png");
		m_toolStrip.addMember(imgButton);

		LayoutSpacer layoutSpacer = new LayoutSpacer();
		layoutSpacer.setWidth("200px");
		m_toolStrip.addMember(layoutSpacer);

		for (String[] link : links) {	
			ToolStripButton toolStripButton = new ToolStripButton(Localization
					.getInstants().getString(link[0]));
			toolStripButton.setID(link[1]);
			toolStripButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					ToolStripButton eventButton = (ToolStripButton)event.getSource();
					String eventButtonID = eventButton.getID();
					m_htmlPanel.setContentsURL(eventButtonID + ".html");
				}
			});
			m_toolStrip.addButton(toolStripButton);		
		}
	}
}
